package com.ssm.zhihu_ssm.service.impl;

import com.ssm.zhihu_ssm.dao.AnswerDao;
import com.ssm.zhihu_ssm.entity.Answer;
import com.ssm.zhihu_ssm.service.AnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import java.util.List;

@Service(value = "AnswerService")
public class AnswerServiceImpl implements AnswerService {

    @Resource
    private AnswerDao answerDao;

//    @Override//根据用户id得到用户所有回答
//    public List<Answer> getAnswerByUsrId(int id) {
//        return answerDao.getAnswerByUserId(id);
//    }

    @Override
    public void insertAnswer(int uid, int question_id, String content) {
        answerDao.insertAnswer(uid, question_id, content);
    }

    @Override
    public void collectAnswer(int uid, int answer_id) {
        answerDao.collectAnswer(uid, answer_id);
        answerDao.addCollectAnswer(answer_id);
    }

    @Override//根据文章id给文章点赞
    public void addAnswerGoodNumByAnswerId(int answer_id, boolean isAdd) {
        if(isAdd == true) {
            answerDao.addAnswerGoodNumByAnswerId(answer_id);
        }else {
            answerDao.minusAnswerGoodNumByAnswerId(answer_id);
        }
    }

    @Override//根据文章id给文章点踩
    public void addAnswerBadNumByAnswerId(int answer_id, boolean isAdd) {
        if(isAdd == true) {
            answerDao.addAnswerBadNumByAnswerId(answer_id);
        }else {
            answerDao.minusAnswerBadNumByAnswerId(answer_id);
        }
    }

    @Override//根据回答id得到回答
    public List<Answer> getAnswerByAnswerId(int answer_id) {
        return answerDao.getAnswerByAnswerId(answer_id);
    }

    @Override//根据问题id得到回答
    public List<Answer> getAnswerByQuestionId(int question_id) {
        return answerDao.getAnswerByQuestionId(question_id);
    }

    @Override//根据话题id获得 按点赞排序的回答
    public List<Answer> getHotSortAnswer(int classify_id) {
        List<Answer> answerList0 = answerDao.getAnswerByClassifyId(classify_id);
        List<Answer> answerList = answerList0.stream().sorted(Comparator.comparingInt(Answer::getGood_num).reversed()).collect(Collectors.toList());
        return answerList;
    }

    @Override
    public void deleteAnswerByAnswerId(int id) {
        answerDao.deleteAnswerByAnswerId(id);
    }

    @Override
    public List<Answer> getAllAnswer() {
        return answerDao.getAllAnswer();
    }
}
